package com.lzb.auth.web.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.joda.time.LocalDateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.lzb.auth.domain.MngUser;

@Controller
@RequestMapping("/site")
public class SiteController {

	public SiteController() {}
	
	@RequestMapping(value="/index1",method=RequestMethod.GET)  
    public ModelAndView index1(){
        ModelAndView modelAndView = new ModelAndView("/testUser");
        //${ requestScope.name }获取
        modelAndView.addObject("name", "lizebin");
        MngUser user = new MngUser();
        user.setAdmin("YES");
        user.setCreateDate(LocalDateTime.now().toString("yyyyMMdd"));
        Map<String, MngUser> map = new HashMap<String, MngUser>();
        map.put("user", user);
        modelAndView.addAllObjects(map);
        //modelAndView.setView((new RedirectView("../admin/login.jsp")));
        return modelAndView;  
    }
	
	//http://127.0.0.1:8080/ssm_gradle/site/index2.do?method=index2
	@RequestMapping(value="/index2", method=RequestMethod.GET, params="method=index2")  
    public ModelAndView index2(){
        ModelAndView modelAndView = new ModelAndView("/testUser");
        //${ requestScope.name }获取
        modelAndView.addObject("name", "lizebin");
        MngUser user = new MngUser();
        user.setAdmin("YES");
        user.setCreateDate(LocalDateTime.now().toString("yyyyMMdd"));
        Map<String, MngUser> map = new HashMap<String, MngUser>();
        map.put("user", user);
        modelAndView.addAllObjects(map);
        return modelAndView;  
    }  
	
	//WebUtils.setSessionAttribute(request, "userid", loginUser.getUserId());
	@RequestMapping(value="/index3", method=RequestMethod.GET)
    public ModelAndView index3(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("/testUser");
        MngUser user = new MngUser();
        user.setAdmin("YES");
        user.setCreateDate(LocalDateTime.now().toString("yyyyMMdd"));
        WebUtils.setSessionAttribute(request, String.valueOf(user.getId()), user);
        List<Object> objectLst = new ArrayList<Object>();
        return modelAndView;  
    }  
	
	public static void main(String[] args) {
		try {
			ServerSocket server = new ServerSocket(38960);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		List<Integer> a = new ArrayList<Integer>();
		a.add(new Integer("1"));
		List<Object> b = new ArrayList<Object>();
		b.add("a");
		a = (List)b;
		System.out.println(b.get(0));
		try {
			Integer c = a.get(0);
		} catch(Exception e) {
			/*ByteArrayOutputStream buf = new ByteArrayOutputStream();
			PrintWriter pw = new PrintWriter(buf);
			e.printStackTrace(pw);
			String s = "";
			pw.println(s);
			System.out.println(s);*/
			 ByteArrayOutputStream buf = new ByteArrayOutputStream();
			 e.printStackTrace(new PrintWriter(buf, true));
			 String expMessage = buf.toString();
			 System.out.println(expMessage);
		}
		System.out.println(a.get(0));
	}
}
